package com.bloveoacom.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.bloveoacom.delegate.IFragment;
import com.bloveoacom.util.ActivityUtil;
import com.bloveoacom.util.LoadDialogUtil;
import com.bloveoacom.util.TUtil;
import com.bloveoacom.exception.ApiException;
import com.bloveoacom.mvp.BasePresenter;
import com.bloveoacom.mvp.IModel;
import com.bloveoacom.mvp.IView;
import com.bloveoacom.weichi.R;
import com.bloveoacom.weichi.activity.LoginActivity;
import com.gyf.barlibrary.ImmersionBar;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * author: yjl
 * e-mail: 1271901673@qq.com
 * time  : 2018/9/20
 * desc  :
 */
public abstract class BaseFragment<P extends BasePresenter, M extends IModel> extends MySupportFragment  implements
       IFragment, IView {
    protected Activity mContext;
    protected View mContentView;
    protected P mPresenter;
    protected M mModel;
    protected ImmersionBar mImmersionBar;
    private Unbinder unbinder;
    private CompositeDisposable mCompositeDisposable;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mContentView == null) {
            init();
        } else {
            ViewGroup parent = (ViewGroup) mContentView.getParent();
            if (parent != null) {
                parent.removeView(mContentView);
            }
        }
        return mContentView;
    }

    private void init() {
        mContentView = LayoutInflater.from(mContext).inflate(getLayoutResId(), null);
        unbinder = ButterKnife.bind(this, mContentView);
        Bundle extras = getArguments();
        if (null != extras) {
            getBundleExtras(extras);
        }
        try {
            mPresenter = TUtil.getT(this, 0);
            mModel = TUtil.getT(this, 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (mPresenter != null) {
            mPresenter.onAttached(this, mModel);
        }
        initView();
        initData();
        initListener();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setTitilBar();
    }

    private void setTitilBar(){
        if(getViewSetTB()!=null){
            ImmersionBar.setTitleBar(getActivity(), getViewSetTB());
        }

    }
    protected View getViewSetTB(){
        return null;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = (Activity) context;
    }


    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        lazyLoad();
    }

    /**
     * 同级下的懒加载＋ViewPager下的懒加载的结合回调方法
     */
    protected void lazyLoad() {
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if (isSetStatusBarAlpha()) {
            mImmersionBar = ImmersionBar.with(this);
            mImmersionBar.keyboardEnable(true)
                    .statusBarDarkFont(isDarkMode(), 0.5f);
            if(isSetBarColor()){
               // mImmersionBar.barColor(R.color.color_bg);
            }
            mImmersionBar.init();
        }
    }
    public boolean isSetBarColor(){
        return true;
    }

    protected boolean isDarkMode() {
        return true;
    }
    public boolean isSetStatusBarAlpha() {
        return true;
    }

    @Override
    public void onDestroy() {
        hideLoadingDialog();
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
        if (mPresenter != null) {
            mPresenter.onDestroy();
        }

        if (mImmersionBar != null) {
            mImmersionBar.destroy();
        }
        unDispose();
    }

    protected void unDispose() {
        if (mCompositeDisposable != null && !mCompositeDisposable.isDisposed()) {
            mCompositeDisposable.dispose();
        }
    }

    protected void addDispose(Disposable disposable) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.add(disposable);
    }
    protected boolean isLogin() {
        if (UserInfoBean.getInstance().isLogin()) {
            return true;
        } else {
            ActivityUtil.startActivity(mContext,LoginActivity.class);
            return false;
        }
    }

    //----------------------------提示------------------------------
    @Override
    public void showLoadingDialog(String title) {
        LoadDialogUtil.showWaitDialog(mContext,title);
    }

    @Override
    public void showLoadingDialog() {
        LoadDialogUtil.showWaitDialog(mContext);
    }

    @Override
    public void showLoadingDialog(String title, boolean isCancel) {
        LoadDialogUtil.showWaitDialog(mContext,title,isCancel);
    }

    @Override
    public void hideLoadingDialog() {
        LoadDialogUtil.dismissWaitDialog();
    }

    @Override
    public void showMessage(String message) {
        ToastUtils.showShort(message);
    }

    @Override
    public void showMessage(int resId) {
        ToastUtils.showShort(resId);
    }

    @Override
    public void showErrorException(ApiException e) {
        ToastUtils.showShort(e.getMessage());
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void showRefreshLoading() {

    }
}
